﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Delivery.Controller;
using DataAccess;
using Business;
using System.Data;
using Delivery.Utils;

namespace Delivery
{
    static class Program
    {
        public static rbfManagement rbfQuanLy;
        public static int user_id;
        public static string LinkLienHe = string.Empty;

        public static string ServerName = string.Empty;
        public static string DatabaseName = string.Empty;
        public static string UserName = string.Empty;
        public static string Password = string.Empty;
        public static string LoginName = string.Empty;
        [STAThread]
        static void Main()
        {

            DataTable dt = new DataTable();
            ParametersBUS pBUS = new ParametersBUS();
            FileSystem fs = new FileSystem();
            if (fs.Read_File() == "0")
            {
                frmCauHinhDuLieu fsc = new frmCauHinhDuLieu();
                fsc.ShowDialog();

            }
            else
            {
                try
                {

                    dt = pBUS.ColorForm();

                    DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = dt.Rows[0]["parameter_name"].ToString();

                    DevExpress.UserSkins.BonusSkins.Register(); // Đưa thư viện này vào để có được các Skin như Valentine, Seven, Summer 2008...
                    DevExpress.UserSkins.OfficeSkins.Register();
                    DevExpress.Skins.SkinManager.EnableFormSkins();
                    DevExpress.LookAndFeel.LookAndFeelHelper.ForceDefaultLookAndFeelChanged();
                }
                catch (Exception) { }

                MyRegistry _myRegistry = new MyRegistry();

                ServerName = _myRegistry.RegGetValue("ServerName");
                DatabaseName = _myRegistry.RegGetValue("DatabaseName");
                UserName = _myRegistry.RegGetValue("UserName");
                Password = _myRegistry.RegGetValue("Password");

                if (ServerName == null || DatabaseName == null)
                {
                    frmThamSoKetNoi frmConnect = new frmThamSoKetNoi();
                    frmConnect.ShowDialog();
                }
                else
                {
                    bool _isConnected = false;
                    System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
                    conn.ConnectionString = "Data Source=" + ServerName + ";Initial Catalog=" + DatabaseName + ";Persist Security Info=True;User ID=" + UserName + ";Password=" + Password;
                    try
                    {
                        conn.Open();
                        _isConnected = true;
                    }
                    catch (Exception)
                    {
                        frmThamSoKetNoi frmConnect = new frmThamSoKetNoi();
                        frmConnect.ShowDialog();
                    }
                    finally
                    {
                        conn.Close();
                    }
                    if (_isConnected)
                    {
                        frmDangNhap dn = new frmDangNhap();
                        dn.ShowDialog();
                        string color = DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName;
                        if (color != dt.Rows[0]["parameter_name"].ToString())
                            pBUS.Update_ColorForm(color);
                    }
                }

            }
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new rbfManagement());
        }
    }
}
